package com.txby.platformApi.component.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jck.common.util.BeanToMapUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.R;
import com.txby.platformCore.entity.SysDept;
import com.txby.platformCore.entity.SysDeptPerson;
import com.txby.platformCore.entity.SysDeptPersonRelation;
import com.txby.platformCore.entity.SysDeptRegionRelation;

/**
 * 代理商组织
 */
@RestController
@RequestMapping("/sys/agent")
public class AgentController  extends CommonController {
	
	/**
	 * region-ztree
	 */
	@RequestMapping("/areaSelect")
	public R areaSelect(){
		String sql="select id,level,code,parentCode,regionName as name from sys_chinaRegion where `level` <= 3";
		List areaList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("areaList", areaList);
	}

	
	/**
	 * dept-ztree
	 */
	@RequestMapping("/deptSelect")
	public R deptSelect(){
		String sql="SELECT dept_id as Id,parent_id as pId ,name FROM sys_dept where type in(0,6)";
		List deptList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("deptList", deptList);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:agent:list")
	public List<SysDept> list(){
		String sql="select * FROM t_txby_sysDeptRegionRelation t1, sys_dept_personRelation t2, sys_dept t3, sys_chinaRegion t4, sys_dept_person t5 WHERE t1.deptId = t3.dept_id AND t1.regionId = t4.id_ AND t2.deptId = t1.deptId AND t2.deptPersonId = t5.id_ AND t3.type in(0,6)";
		List deptList=this.mBaseDao.findBySql(sql, Map.class, null);
		return deptList;
	}
	
	
	/**
	 * 上级组织Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:agent:list")
	public R info(){
		long deptId = 0;
		long selfId = 0;
		String name = "";
		if(getUserId() != Constant.SUPER_ADMIN){
			SysDept dept = 
			(SysDept) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysDept.class)+" where dept_id = "+getDeptId(), SysDept.class, null);
			deptId = dept.getParent_id();
			selfId=dept.getDept_id();
			name = dept.getName();
		}

		return R.ok().put("deptId", deptId).put("selfId", selfId);
	}
	
	
	/**
	 * 保存
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public R save(@RequestBody Map dept) throws Exception{
		// System.out.println(dept);
		if (dept.size()>0) {
		 // Map map = new HashMap<>();//dept
		  Map map2 = new HashMap<>();//联系人map
		  Map map3 = new HashMap<>();//区域map
		  Map map4 = new HashMap<>();
		  SysDept sysDept = new SysDept();//dept组织
		  
		  String dept_name = dept.get("name").toString();
		  String dept_parentId = dept.get("parentId").toString();
		  
		  String sql="select * from "+this.mBaseDao.tableName(SysDept.class)+" where name='"+dept_name+"' and parent_id ='"+dept_parentId+"'";
		  SysDept s=(SysDept) this.mBaseDao.findBySqlUniqueObject(sql, SysDept.class, null);
			 if(s!=null){
				 return R.ok().error("代理商重复");
			 }else{
				  
				  sysDept.setDel_flag(new Integer((dept.get("del_flag")).toString()));
				  sysDept.setName(dept_name);
				  sysDept.setOrder_num(new Integer((dept.get("orderNum")).toString()));
				  sysDept.setParent_id(new Long(dept_parentId));
				  sysDept.setType(new Integer((dept.get("type")).toString()));
				  this.mBaseDao.saveObject(sysDept);
			 }
		  
		  map2.put("linkname", dept.get("linkname"));//联系人
		  map2.put("duty", dept.get("duty"));//职务
		  map2.put("mobileNO", dept.get("mobileNO"));//手机号
		  
		  map3.put("address", dept.get("address"));
		  
		  SysDeptPerson sysDeptPerson = (SysDeptPerson) BeanToMapUtil.convertMap(SysDeptPerson.class, map2);
		  SysDeptRegionRelation sysDeptRegionRelation = (SysDeptRegionRelation) BeanToMapUtil.convertMap(SysDeptRegionRelation.class, map3);
		  sysDeptPerson.setCreater(getUserId().toString());
		  sysDeptPerson.setUpdater(getUserId().toString());
		 // System.out.println(JSON.toJSONString(sysDept));
		  this.mBaseDao.saveObject(sysDeptPerson);
		  
		  map4.put("deptPersonId", sysDeptPerson.getId_());
		  
		  sysDeptRegionRelation.setDeptId(sysDept.getId_());
		  sysDeptRegionRelation.setCreater(getUserId().toString());
		  sysDeptRegionRelation.setUpdater(getUserId().toString());
		  sysDeptRegionRelation.setRegionId(new Long(dept.get("regionId").toString()));
		  this.mBaseDao.saveObject(sysDeptRegionRelation);
		  
		  
		  SysDeptPersonRelation sysDeptPersonRelation = (SysDeptPersonRelation) BeanToMapUtil.convertMap(SysDeptPersonRelation.class, map4);
		  sysDeptPersonRelation.setDeptId(sysDept.getId_());
		  sysDeptPersonRelation.setCreater(getUserId().toString());
		  sysDeptPersonRelation.setUpdater(getUserId().toString());
		  this.mBaseDao.saveObject(sysDeptPersonRelation);
		  
		}
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT * FROM t_txby_sysDeptRegionRelation t1, sys_dept_personRelation t2, sys_dept t3, sys_chinaRegion t4, sys_dept_person t5 WHERE t1.deptId = t3.dept_id AND t1.regionId = t4.id_ AND t2.deptId = t1.deptId AND t2.deptPersonId = t5.id_ and t3.dept_id ="+id;
		Map agent =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("agent", agent);
	}
	
	
	/**
	 * 上级
	 */
	@RequestMapping("/parentName/{parent_id}")
	public R parentName(@PathVariable("parent_id") Long parent_id){
		String sql="SELECT name FROM sys_dept WHERE dept_id = "+parent_id;
		Map parent =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("parent", parent);
	}
	
	
	/*
	 * 
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:agent:update")
	public R update(@RequestBody Map dept) throws Exception{
			  String dept_name = dept.get("name").toString();
			  String dept_parentId = dept.get("parent_id").toString();
			  String dept_flag = dept.get("del_flag").toString();
			  String dept_order_num = dept.get("orderNum").toString();
			  String dept_type =dept.get("type").toString();
			  String dept_id =dept.get("dept_id").toString();
			  String deptsql = "select * from "+this.mBaseDao.tableName(SysDept.class)+" where name='"+dept.get("parentName").toString()+"'";
			  //上级
			  SysDept deptparent=(SysDept) this.mBaseDao.findBySqlUniqueObject(deptsql, SysDept.class, null);
			  String sql="select * from "+this.mBaseDao.tableName(SysDept.class)+" where name='"+dept_name+"' and parent_id ='"+dept_parentId+"' and dept_id !="+dept_id;
			  SysDept s=(SysDept) this.mBaseDao.findBySqlUniqueObject(sql, SysDept.class, null);
				 if(s!=null){
					 return R.ok().error("代理商重复");
				 }else{
					sql = "update sys_dept set parent_id="+deptparent.getDept_id()+","
							+ "name='"+dept_name+"',order_num="+dept_order_num+
							 " where  dept_id= "+dept_id ; 
					this.mBaseDao.executeSql(sql, null);
				 }
			String sql2 = "select deptPersonId from sys_dept_personRelation where deptId = "+dept_id;
			Map map = (Map) this.mBaseDao.findBySqlUniqueObject(sql2, Map.class, null);
			String dept_person_id  = map.get("deptPersonId").toString();
			String linkname = dept.get("linkname").toString();
			String duty= dept.get("duty").toString();
			String mobileNO= dept.get("mobileNO").toString();
			
			String personsql = "update sys_dept_person set linkname = '"+linkname+"',"
					+ "duty = '"+duty+"',mobileNO = '"+mobileNO+"' where id_="+dept_person_id;
			this.mBaseDao.executeSql(personsql, null);
			
			String regionId = dept.get("regionId").toString();
			String address = dept.get("address").toString();
			
			String regionsql ="update t_txby_sysDeptRegionRelation set address = '"+address+"',"
					+ "regionId = '"+regionId+"' where deptId="+dept_id;
			this.mBaseDao.executeSql(regionsql, null);
		
			return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:agent:delete")
	public R delete(@RequestBody Map id){
		 		String ids = id.get("dept_id").toString();
			    String sql = "DELETE t1, t2, t3, t5 FROM t_txby_sysDeptRegionRelation t1, sys_dept_personRelation t2, sys_dept t3, sys_chinaRegion t4, sys_dept_person t5 WHERE t1.deptId = t3.dept_id AND t1.regionId = t4.id_ AND t2.deptId = t1.deptId AND t2.deptPersonId = t5.id_ AND t3.dept_id ="+ids;
		    	this.mBaseDao.executeSql(sql, null);
			return R.ok();
	}
	
	
	
}
